package com.peakproducts;

import org.json.me.util.Utils;

import com.peakproducts.AnimatedGIFField;

import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.Status;

public class LoginScreen extends PeakScreen {
	private BasicEditField loginname = new BasicEditField("Contractor ID:", "");
	private BasicEditField salesRepId = new BasicEditField("Sales Rep. ID:",
			"administrator");
	private PasswordEditField password = new PasswordEditField("Password:", "");
	private PeakField loginButton = new PeakField("Login");
	private ConnectionThread connThread;

	public LoginScreen(ConnectionThread ct) {
		connThread = ct;
		setTitle("Lead Manager - Login");
		try{
			AnimatedGIFField gif = new AnimatedGIFField((GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("logo_login.gif"));
			add(gif);
		}catch (Exception e) {
			// TODO: handle exception
			Status.show(e.getMessage());
		}
		add(loginname);
		add(salesRepId);
		add(password);
		add(new SeparatorField());
		add(loginButton);
		FieldChange fieldChange = new FieldChange();
		loginButton.setChangeListener(fieldChange);
	}

	public class FieldChange implements FieldChangeListener {
		public void fieldChanged(Field field, int context) {
			StringBuffer sb = new StringBuffer("Sending...");
            connThread.get(Utils.restURL+"/Contractor/Login"+getFieldData());
            while (connThread.sending) {
                try {
                    Status.show( sb.append(".").toString() );
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (connThread.sendResult) {
                Status.show("Transmission Successful");
                //xmlResponse.setText( connThread.responseContent );
                parseResults( connThread.responseContent );
            } else {
                Status.show("Transmission Failed");
            }
		}

		private String getFieldData() {
			StringBuffer sb = new StringBuffer();
	        sb.append("/id/");
	        sb.append(loginname.getText());
	        sb.append("/username/");
	        sb.append(salesRepId.getText());
	        sb.append("/password/");
	        sb.append(password.getText());
	        return sb.toString();
		}

		private void parseResults(String responseContent) {
			//Status.show(responseContent);
			if(responseContent.equals("0")){
				Status.show("Login Failed, please check your account and password.");
			}else{
				Utils.contractor_id = loginname.getText();
				LeadSearchScreen searchScreen = new LeadSearchScreen(connThread);
				UiApplication.getUiApplication().pushScreen(searchScreen);
				close();
			}
		}
	}
}
